package github_rss

import javax.servlet.http._

class Main extends HttpServlet {
  import Main._
  
  override def doGet( req:HttpServletRequest,  resp:HttpServletResponse){
    val o = (getRSSData \ "entry").map{ UserAction.apply }
    val oldDataIdList = DB.selectAll
    val newData = o.filterNot{a => oldDataIdList.contains(a.id)}
    DB.insert(newData:_*)
    val client = TweetClient(Settings.MyRSSbotSetting)
    newData.take(5).foreach{ Thread.sleep(500) ; client.tweet }
    resp.getWriter().write(newData.map{_.tweetString}.mkString("\n\n"))
  }
}

object Main{
  
  private val MyGithubRSS = new java.net.URL( 
    """http://scala-users.org/shibuya/index.php?title=%e7%89%b9%e5%88%a5:%e6%9c%80%e8%bf%91%e3%81%ae%e6%9b%b4%e6%96%b0&feed=atom"""
  )
  
  def getRSSData:xml.Elem = {
    xml.XML.load(MyGithubRSS)
  }
  
}